import Flutter
import UIKit
public class BusinessPlugin: NSObject, FlutterPlugin {
  // 添加静态通道变量
  private static var channel: FlutterMethodChannel?
  public static var tokenString: String?
   // 添加 stateHandler 属性
  private var stateHandler: ApplicationStateHandler?
  public static func register(with registrar: FlutterPluginRegistrar) {
     channel = FlutterMethodChannel(name: "com.cqxm.app/BusinessKitChannel", binaryMessenger: registrar.messenger())
    let instance = BusinessPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel!)

    // 初始化应用状态处理器
    instance.stateHandler = ApplicationStateHandler()
    instance.stateHandler?.setup(messenger: registrar.messenger())
  }
  
  // 处理从 Flutter 发来的方法调用
  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    switch call.method {
    default:
      // 处理其他方法调用...
      result(FlutterMethodNotImplemented)
    }
  }

  // 添加一个静态方法用于传递推送令牌
  public static func setPushToken(token: Data) {
    // 将 Data 转换为十六进制字符串
    tokenString = token.map { String(format: "%02x", $0) }.joined()
    // 通过通道将令牌传递给 Flutter
    channel?.invokeMethod("setPushToken", arguments: tokenString)
  }
}
